<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="card-header">
        <h2 class="login-title">用户登录</h2>
      </div>
      
      <el-form 
        ref="loginFormRef" 
        :model="loginForm" 
        :rules="loginRules" 
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="User"
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="Lock"
          ></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="login-button" 
            @click="handleLogin">
            登录
          </el-button>
        </el-form-item>
        
        <div class="register-link">
          还没有账号? 
          <el-link @click="$router.push('/register')">立即注册</el-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup name="Login">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus' 
import { useUserStore } from '../store/user'

const router = useRouter()
const userStore = useUserStore()

// 登录表单数据
const loginForm = ref({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
})

// 表单引用
const loginFormRef = ref()

// 处理登录
const handleLogin = async () => {
    loginFormRef.value.validate(async (valid) => {
        if (valid) {
            const result = await userStore.login(loginForm.value)
            if (result.success) {
                ElMessage.success('登录成功')
                router.push('/home')
            } else {
                ElMessage.error(result.message || '用户名不存在或密码错误')
                loginForm.value.password = ''
            }
        }
    })
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../assets/login.jpg') no-repeat center center; 
  background-size: cover;
}

.login-card {
  width: 400px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 0; 
  border-radius: 8px;
  overflow: hidden;
  max-width: 90%; 
}

.card-header {
  text-align: center;
  padding: 20px 0;
  background-color: #fff;
}

.login-title {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: #f56c6c;
}

.login-form {
  padding: 20px;
}

.el-input__wrapper {
  border-radius: 4px;
}

.login-button {
  width: 100%;
  background-color: #f56c6c;
  border: none;
}
.login-button:hover {
  background-color: #e64949;
}

.register-link {
  text-align: center;
  margin-top: 0;
  padding-bottom: 20px;
  background-color: #fff;
}
.register-link a {
  color: #f56c6c;
  text-decoration: none;
}
.register-link a:hover {
  text-decoration: underline;
}
</style>